"use strict";
define(["commonUI","getTime","laydate","global", "datatables"], function (
    commonUI, getTime,laydate,global
) {
    //当前登录用户
    var user=null;
    //登录日志表格、操作日志表格
    var standardTable1, standardTable2;
    //登录日志参数
    var param1 = {
        pageNumber: 1,
        pageSize: 10
    };
    //操作日志参数
    var param2 = {
        pageNumber: 1,
        pageSize: 10
    };
    
    var app = {
        init: function () {
            user = global.getLoginUser();
            this.initText();
            this.eventBind();
            // getTime.resize();
            getTime.getNowDate();
            getTime.getTimeInfo();
            this.initText();
            this.initSelect1();
            this.initTable1();
            this.initSelect2();
            this.initTable2();
        },
        /**
        * 渲染文字内容
        */
        initText: function() {
            user = global.getLoginUser();
            //用户名显示
            $(".name").text(user.userName)
        },
        // 搜索条件初始化
        initSelect1: function () {
            var start = laydate.render({
                elem: '#beginTime',
                showBottom: false,
                eventElem: '.layout-icon-1',
                trigger: 'click',
                // max: 0,
                done: function (value, date, endDate) {
                    end.config.min = {
                        year: date.year,
                        month: date.month - 1,
                        date: date.date
                    }; 
                    end.config.value = {
                        year: date.year,
                        month: date.month - 1,
                        date: date.date
                    }; //将结束日的初始值设定为开始日
                }
            });
            //结束时间
            var end = laydate.render({
                elem: '#endTime',
                showBottom: false,
                eventElem: '.layout-icon-2',
                trigger: 'click',
                // max: 0,
                done: function (value, date) {
                    start.config.max = {
                        year: date.year,
                        month: date.month - 1,
                        date: date.date
                    }; //结束日选好后，重置开始日的最大日期
                }
    
            })
        },
        initSelect2: function () {
            var start = laydate.render({
                elem: '#beginTime2',
                showBottom: false,
                eventElem: '.layout-icon-3',
                trigger: 'click',
                // max: 0,
                done: function (value, date, endDate) {
                    end.config.min = {
                        year: date.year,
                        month: date.month - 1,
                        date: date.date
                    }; 
                    end.config.value = {
                        year: date.year,
                        month: date.month - 1,
                        date: date.date
                    }; //将结束日的初始值设定为开始日
                }
            });
            //结束时间
            var end = laydate.render({
                elem: '#endTime2',
                showBottom: false,
                eventElem: '.layout-icon-4',
                trigger: 'click',
                // max: 0,
                done: function (value, date) {
                    start.config.max = {
                        year: date.year,
                        month: date.month - 1,
                        date: date.date
                    }; //结束日选好后，重置开始日的最大日期
                }
    
            })
        },
        // 表格初始化
        initTable1: function () {
            standardTable1 = $("#standard_table1").DataTable({
                // 关闭搜索功能
                processing: true,
                searching: false,
                serverSide: true,
                destroy: true,
                ordering: false,
                // scrollY: 600,
                // scrollCollapse: true,
                columns: [{
                    title: "序号",
                    data: "number",
                    width: "100px",
                    render: function (data, type, full) {
                        return "<div class='rank'>" + data + "</div>";
                    }
                }, {
                    title: "操作用户",
                    data: "userName",
                    width: "20%",
                    render: function (data, type, full) {
                        if(full.sex=="1") {
                            return "<div class=\"text-overflow male\" title=\"" + data + "\"><i class='aidicon aidicon-account-circle'></i>" + data + "</div>";
                        } else {
                            return "<div class=\"text-overflow female\" title=\"" + data + "\"><i class='aidicon aidicon-account-circle'></i>" + data + "</div>";
                        }
                        
                    }
                }, {
                    title: "用户类型",
                    data: "userType",
                    width: "150px",
                    render: function (data, type, full) {
                        if(data=="管理员") {
                            return "<span class=\"type-label label2\" title=\"" + data + "\">" + data + "</span>";
                        } else if(data=="超级管理员") {
                            return "<span class=\"type-label label3\" title=\"" + data + "\">" + data + "</span>";
                        } else {
                            return "<span class=\"type-label label1\" title=\"" + data + "\">" + data + "</span>";
                        }
                    }
                }, {
                    title: "登录时间",
                    data: "loginTime",
                    width: "30%"
                }, {
                    title: "退出时间",
                    data: "logoutTime",
                    width: "30%",
                    render:function(data,type,full){
                        if(data=="null"){
                            return "<span>-</span>";
                        }
                        return data;
                    }
                }],
                columnDefs:[{
                    // targets 同时制定多个列时，可以传递数组（比如[1,2]）;制定所有传"_all"  
                    targets: [3,4],
                    render: function(data,type,full,meta){//其中data代表本列数据，full代表本行所有数据
                        return "<div class=\"text-overflow\" title=\"" + data + "\">" + data + "</div>";
                    }
                }],
                language: {
                    sEmptyTable: "<div class=\"blank-icon\"><p class=\"msg\"><i class=\"aidicon aidicon-emoticon-sad\"></i> 没有搜到任何数据</p></div>"
                },
                ajax: function (params, callback, setting) {
                    param1.pageNumber = (params.start / params.length) + 1;
                    param1.pageSize = params.length;
                    param1.startTimeSearch = $("#beginTime").val();
                    param1.endTimeSearch = $("#endTime").val();
                    param1.keySearch = $("#optUser").val();
                    global.get("/web/log/loginlog/list",function (res) {
                        if (res.success) {
                            var obj = {};
                            obj.data = res.data.page.content;
                            obj.draw = params.draw;
                            obj.recordsTotal = res.data.page.totalElements;
                            obj.recordsFiltered = res.data.page.totalElements;
                            callback(obj);
                            }
                        },param1);
                },
                initComplete: function (settings, json) {

                }
            });
        },
        // 表格初始化
        initTable2: function () {
            standardTable2 = $("#standard_table2").DataTable({
                // 关闭搜索功能
                processing: true,
                searching: false,
                serverSide: true,
                destroy: true,
                ordering: false,
                // scrollY: 600,
                // scrollCollapse: true,
                columns: [{
                    title: "序号",
                    data: "number",
                    width: "100px",
                    render: function (data, type, full) {
                        return "<div class='rank'>" + data + "</div>";
                    }
                }, {
                    title: "操作用户",
                    data: "userName",
                    width: "30%",
                    render: function (data, type, full) {
                        if(full.sex=="1") {
                            return "<div class=\"text-overflow male\" title=\"" + data + "\"><i class='aidicon aidicon-account-circle'></i>" + data + "</div>";
                        } else {
                            return "<div class=\"text-overflow female\" title=\"" + data + "\"><i class='aidicon aidicon-account-circle'></i>" + data + "</div>";
                        }
                        
                    }
                }, {
                    title: "用户类型",
                    data: "userType",
                    width: "150px",
                    render: function (data, type, full) {
                        if(data=="管理员") {
                            return "<span class=\"type-label label2\" title=\"" + data + "\">" + data + "</span>";
                        } else if(data=="超级管理员") {
                            return "<span class=\"type-label label3\" title=\"" + data + "\">" + data + "</span>";
                        } else {
                            return "<span class=\"type-label label1\" title=\"" + data + "\">" + data + "</span>";
                        }
                    }
                }, {
                    title: "操作时间",
                    data: "operationTime",
                    width: "30%"
                }, {
                    title: "操作内容",
                    data: "operationContent",
                    width: "40%"
                }],
                columnDefs:[{
                    // targets 同时制定多个列时，可以传递数组（比如[1,2]）;制定所有传"_all"  
                    targets: [3,4],
                    render: function(data,type,full,meta){//其中data代表本列数据，full代表本行所有数据
                        return "<div class=\"text-overflow\" title=\"" + data + "\">" + data + "</div>";
                    }
                }],
                language: {
                    sEmptyTable: "<div class=\"blank-icon\"><p class=\"msg\"><i class=\"aidicon aidicon-emoticon-sad\"></i> 没有搜到任何数据</p></div>"
                },
                ajax: function (params, callback, setting) {
                    param2.pageNumber = (params.start / params.length) + 1;
                    param2.pageSize = params.length;
                    param2.startTimeSearch = $("#beginTime2").val();
                    param2.endTimeSearch = $("#endTime2").val();
                    param2.operationUser = $("#optUser").val();
                    param2.operation = $("#content").val();
                    global.get("/web/log/operationlog/list",function (res) {
                        if (res.success) {
                            var obj = {};
                            obj.data = res.data.page.content;
                            obj.draw = params.draw;
                            obj.recordsTotal = res.data.page.totalElements;
                            obj.recordsFiltered = res.data.page.totalElements;
                            callback(obj);
                            }
                        },param2);
                },
                initComplete: function (settings, json) {

                }
            });
        },
        /** 事件绑定 */
        eventBind: function () {
            $(document).on("click", ".tab-nav li", function () {
                $(this).addClass("active").siblings("li").removeClass("active");
                var index = $(this).attr("type");
                $(".tab-wrapper").eq(index-1).removeClass("hidden").siblings(".tab-wrapper").addClass("hidden");
            })
            //登录日志搜索按钮
            $(document).on("click", "#searchBtn1", function () {
                // 改变param1参数
                standardTable1.ajax.reload();
            })
            //操作日志搜索按钮
            $(document).on("click", "#searchBtn2", function () {
                // 改变param2参数
                standardTable2.ajax.reload();
            })
            window.onresize = function () {
                // getTime.resize();
            };
        },
        
    };
    return app;
});